<?php

/* -----------------------------------------------------------------
 * 	$Id: blog_categories_list.php 959 2014-04-10 05:31:01Z sbraeutig $
 * 	Copyright (c) 2011-2021 commerce:SEO by Webdesign Erfurt
 * Copyright by H&S eCom 
 * @author little Pit(S.B.)
 * 	http://www.commerce-seo.de : www.indiv-style.de
 * ------------------------------------------------------------------
 * 	based on:
 * 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
 * 	(c) 2002-2003 osCommerce - www.oscommerce.com
 * 	(c) 2003     nextcommerce - www.nextcommerce.org
 * 	(c) 2005     xt:Commerce - www.xt-commerce.com
 * 	Released under the GNU General Public License
 * --------------------------------------------------------------- */

defined("_VALID_XTC") or die("Direct access to this location isn't allowed.");
$module_smarty = new Smarty;
$module_smarty->assign('txt', $coo_text_mgr->v_section_content_array['blog']);

if (isset($_GET['ucatid'])) {
    $u_catid = $_GET['ucatid'];
} else {
    $u_catid = '0';
}

$status[] = array('id' => 0, 'text' => TABLE_FOOTER_STATUS_0);
$status[] = array('id' => 1, 'text' => TABLE_FOOTER_STATUS_1);
$status[] = array('id' => 2, 'text' => TABLE_FOOTER_STATUS_2);
$status[] = array('id' => 3, 'text' => TABLE_FOOTER_STATUS_3);

// Edit
$listedit = array();
$listedit[] = array('id' => 0, 'text' => TABLE_HEADING_ACTION_EDIT_0);
$listedit[] = array('id' => 1, 'text' => TABLE_HEADING_ACTION_EDIT);

$select_categories = array();
$categories_query = xtc_db_query("SELECT id, titel FROM " . TABLE_BLOG_CATEGORIES . " WHERE language_id = '" . (int) $_SESSION['languages_id'] . "' ORDER BY position ASC");
$select_categories[] = array('id' => 0, 'text' => TABLE_FOOTER_STATUS_0);

while ($categories = xtc_db_fetch_array($categories_query)) {
    $select_categories[] = array('id' => $categories['id'], 'text' => $categories['titel']);
}
$module_smarty->assign('FORM', xtc_draw_form('leiste', FILENAME_BLOG, 'action=set', 'post', ''));

$categories_query = xtc_db_query("SELECT
											*
			       						FROM
			       							" . TABLE_BLOG_CATEGORIES . "
        								WHERE
        									language_id = '" . (int) $_SESSION['languages_id'] . "'
                                        AND
                                            parent_id = '" . $u_catid . "'
        								ORDER BY
        									position ASC");
if (xtc_db_num_rows($categories_query, true)) {
    $my_cat = 1;
    while ($categories = xtc_db_fetch_array($categories_query)) {
        $count_offline_items_query = xtc_db_query("SELECT COUNT(categories_id) AS number FROM " . TABLE_BLOG_ITEMS . " WHERE categories_id = '" . (int) $categories['categories_id'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "' AND status = '1'");
        $count_offline_items = xtc_db_fetch_array($count_offline_items_query);
        $count_items_query = xtc_db_query("SELECT COUNT(categories_id) AS number FROM " . TABLE_BLOG_ITEMS . " WHERE categories_id = '" . (int) $categories['categories_id'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "'");
        $count_items = xtc_db_fetch_array($count_items_query);
        $catstatus = xtc_draw_selection_field('status[]', 'checkbox', $categories['categories_id']);
        $cat_name = '<span id="cat_' . $categories['id'] . '"><b>' . ($categories['status'] == 0 || $categories['status'] == 1 ? '<span style="color:#9f9f9f">' . $categories['titel'] . '</span>' : $categories['titel']) . '</b>' . ($categories['status'] == 0 || $categories['status'] == 1 ? ' <span style="color:#ff0000"><b>' . TABLE_FOOTER_STATUS_1 . '</b></span>' : '');
        $cat_eigen = '<em style="font-size:85%">' . TABLE_HEADING_BLOG_TOPIC . $count_items['number'] . ', ' . TABLE_HEADING_BLOG_TOPIC_OFF . $count_offline_items['number'] . '</em>';
        $field_position = $categories['position'];
        $linknew = xtc_href_link(FILENAME_BLOG, 'action=new_item&cat=' . $categories['categories_id']);
        $linkshow = xtc_href_link(FILENAME_BLOG, 'action=showitems&cat=' . $categories['categories_id']);
		$linkeditcat = xtc_href_link(FILENAME_BLOG, 'action=editcategories&cat=' . $categories['categories_id']);
        $my_blog_array[$my_cat] = array(
            'catid' => $categories['categories_id'],
            'catname' => $cat_name,
            'cateigen' => $cat_eigen,
            'position' => $field_position,
            'linknew' => $linknew,
            'cat_status' => $catstatus,
            'linknew' => $linknew,
            'linkshow' => $linkshow,
            'linkeditcat' => $linkeditcat,
        );

        $categories2_query = xtc_db_query("SELECT *
										FROM " . TABLE_BLOG_CATEGORIES . "
                                        WHERE language_id = '" . (int) $_SESSION['languages_id'] . "'
                                        AND parent_id = '" . $categories['categories_id'] . "'
                                        ORDER BY position ASC");
        if (xtc_db_num_rows($categories2_query)) {
            $uncats = 0;
            while ($ucats = xtc_db_fetch_array($categories2_query)) {
                $count_offline_items_query = xtc_db_query("SELECT COUNT(categories_id) AS number FROM " . TABLE_BLOG_ITEMS . " WHERE categories_id = '" . (int) $ucats['categories_id'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "' AND status = '1'");
                $count_offline_items = xtc_db_fetch_array($count_offline_items_query);
                $count_items_query = xtc_db_query("SELECT COUNT(categories_id) AS number FROM " . TABLE_BLOG_ITEMS . " WHERE categories_id = '" . (int) $ucats['categories_id'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "'");
                $count_items = xtc_db_fetch_array($count_items_query);
                $catstatus2 = xtc_draw_selection_field('status[]', 'checkbox', $ucats['categories_id']);
                $cat_name2 = '<span id="cat_' . $ucats['categories_id'] . '"><b>' . ($ucats['status'] == 0 || $ucats['status'] == 1 ? '<span style="color:#9f9f9f">' . $ucats['titel'] . '</span>' : $ucats['titel']) . '</b>' . ($ucats['status'] == 0 || $ucats['status'] == 1 ? ' <span style="color:#ff0000"><b>' . TABLE_FOOTER_STATUS_1 . '</b></span>' : '');
                $cat_eigen2 = '<em style="font-size:85%">' . TABLE_HEADING_BLOG_TOPIC . $count_items['number'] . ', ' . TABLE_HEADING_BLOG_TOPIC_OFF . $count_offline_items['number'] . '</em>';
                $field_position2 = $ucats['position'];
                $linknew2 = xtc_href_link(FILENAME_BLOG, 'action=new_item&cat=' . $ucats['categories_id']);
                $linknew_pic2 = xtc_image(DIR_WS_ICONS . 'document--plus.png', 'neuen Beitrag verfassen');
                $linkshow2 = xtc_href_link(FILENAME_BLOG, 'action=showitems&cat=' . $ucats['categories_id']);
                $linkshow_pic2 = xtc_image(DIR_WS_ICONS . 'go.gif', 'In diese Kategorie wechseln');
                $linkeditcat2 = xtc_href_link(FILENAME_BLOG, 'action=editcategories&cat=' . $ucats['categories_id']);
                $linkeditcat_pic2 = xtc_image(DIR_WS_ICONS . 'edit.gif', TABLE_HEADING_ACTION_EDIT);
                $my_blog_array[$my_cat]['un_cats'][$uncats] = array(
                    'catid' => $ucats['categories_id'],
                    'catname' => $cat_name2,
                    'cateigen' => $cat_eigen2,
                    'position' => $field_position2,
                    'linknew' => $linknew2,
                    'cat_status' => $catstatus2,
                    'linknew' => $linknew2,
                    'linknew_pic' => $linknew_pic2,
                    'linkshow' => $linkshow2,
                    'linkshow_pic' => $linkshow_pic2,
                    'linkeditcat' => $linkeditcat2,
                    'linkeditcat_pic' => $linkeditcat_pic2
                );
                $uncats++;
            }
        }


        $getCatItems = xtc_db_query("SELECT name, status, position, item_id FROM blog_items WHERE categories_id = '" . (int) $categories['categories_id'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "' ORDER BY position ASC ");
        if (xtc_db_num_rows($getCatItems)) {
            $isitem = 1;
            while ($items = xtc_db_fetch_array($getCatItems)) {
                $module_smarty->assign('NOT_ITEM', '');
                $link = '<a href="blog.php?action=edit_item&cat=' . $categories['categories_id'] . '&item=' . $items['item_id'] . '"><i class="glyphicon glyphicon-edit"></i></a>';
                $dellink = '<a onclick="return confirm(\'Beitrag wirklich entfernen???\')" href="blog.php?action=delete_item&item=' . $items['item_id'] . '"><i class="glyphicon glyphicon-trash"></i></a>';
                $name = $items['name'];
                if ($items['status'] == '1')
                    $statusl = '<span style="color:#ff0000"><b>' . TABLE_FOOTER_STATUS_1 . '</b></span>';
                elseif ($items['status'] == '2')
                    $statusl = '<span style="color:#046918"><b>' . TABLE_FOOTER_STATUS_2 . '</b></span>';
                $position = $items['position'];
                $itemstatus = xtc_draw_selection_field('status[]', 'checkbox', $items['item_id']);
                $my_blog_array[$my_cat]['items'][$isitem] = array(
                    'itemid' => $items['item_id'],
                    'aktion' => $link,
                    'aktion2' => $dellink,
                    'titel' => $name,
                    'status' => $items['status'],
                    'item_status' => $itemstatus,
                    'position' => $position
                );
                $isitem++;
            }
        } else {
            $module_smarty->assign('NOT_ITEM', '<br />Keine Einträge');
        }
        $my_cat++;
    }
} else {
    $module_smarty->assign('NOTCAT', '<b>Derzeit sind keine Kategorien vorhanden.</b>');
}
$module_smarty->assign('blogstatus', xtc_draw_pull_down_menu('set_cat_status', $status, '', 'class="form-control" style="width:100px; float:left;margin-right:2%"'));
$module_smarty->assign('blogkat', $my_blog_array);
$module_smarty->assign('FORM_END', '</form>');
$module_smarty->assign('language', $_SESSION['language']);
$module_smarty->caching = false;

$module_smarty->template_dir = DIR_FS_CATALOG . 'admin/templates/';
$module_smarty->compile_dir = DIR_FS_CATALOG . 'admin/templates_c';
$mxmodule = $module_smarty->fetch(CURRENT_ADMIN_TEMPLATE . '/blog_categories_list.html');
$smarty->assign('MODULE_blog_categories_list', $mxmodule);
